package com.anquing.socket03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Administrator
 *客户端
 */
public class Client {

	public static void main(String[] args) {
		try {
			//1、创建客户端Socket,指定服务器地址和端口
			Socket socket = new Socket("localhost", 8888);
			//2、获取输出流，向服务器端发送登录的信息
			OutputStream os = socket.getOutputStream();//字节输出流
			PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
			pw.write("用户名:admin;密码:456");
			pw.flush();
			socket.shutdownOutput();
			//3、获取输入流，并读取服务器端的响应信息
			InputStream is = socket.getInputStream();//字节输入流
			InputStreamReader isr = new InputStreamReader(is);//将字节输入流转换成字符流
			BufferedReader br = new BufferedReader(isr);//为输入流添加缓冲
			String info = null;
			while((info = br.readLine())!=null){//循环读取客户端的信息
				System.out.println("我是客户端，服务器说："+info);
			}
			socket.shutdownInput();//关闭输入流
			//4、关闭相关资源
			is.close();
			isr.close();
			br.close();
			pw.close();
			os.close();
			socket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
